package com.app.controller;


import cn.hutool.core.util.StrUtil;
import com.app.model.PatrolGroup;
import com.app.result.CommonResult;
import com.app.service.IPatrolGroupService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 巡察组 前端控制器
 * </p>
 *
 * @author ghoul
 * @since 2024-12-17
 */
@RestController
@RequestMapping("/patrolGroup")
public class PatrolGroupController {
    @Autowired
    private IPatrolGroupService patrolGroupService;

    @GetMapping("/getPerTree")
    public CommonResult<Object> getPerTree(String keyword, Integer type) {
        QueryWrapper<PatrolGroup> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword)) {
            queryWrapper.like("name", keyword);
        }
        if (type == null) {
            queryWrapper.eq("status", 1);
        }
        queryWrapper.eq("is_del", 0);
        queryWrapper.orderByAsc("sort");
        List<PatrolGroup> patrolGroupList = patrolGroupService.list(queryWrapper);//全查或者根据name查
        if (patrolGroupList == null) {
            return CommonResult.failed("暂无数据");
        }
        List<PatrolGroup> patrolGroups = patrolGroupService.getCategoryTree(patrolGroupList);
        return CommonResult.success(patrolGroups);
    }


    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<List<PatrolGroup>> getList(Integer type) {
        return CommonResult.success(patrolGroupService.getAdminList(type));
    }

    /**
     * 新增
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated PatrolGroup request) {
        if (patrolGroupService.create(request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(Integer id) {
        if (patrolGroupService.deleteById(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<PatrolGroup> info(Integer id) {
        return CommonResult.success(patrolGroupService.getDetail(id));
    }

    /**
     * 查询巡察组
     *
     * @return
     */
    @RequestMapping(value = "/getPatrolGroup", method = RequestMethod.GET)
    public CommonResult<List<PatrolGroup>> getPatrolGroup() {
        QueryWrapper<PatrolGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", 0);
        queryWrapper.eq("pid", 0);
        return CommonResult.success(patrolGroupService.list(queryWrapper));
    }

    /**
     * 更新状态
     *
     * @return
     */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.GET)
    public CommonResult<String> updateStatus(Integer id, Integer status) {
        if (patrolGroupService.updateStatus(id, status)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}

